package com.ruoyi.bom.controller;

import com.ruoyi.bom.domain.SysDept;
import com.ruoyi.bom.domain.TqlFood;
import com.ruoyi.bom.domain.TqlFoodUnit;
import com.ruoyi.bom.remoteService.HualalaApiService;
import com.ruoyi.bom.service.SysDeptService;
import com.ruoyi.bom.service.TqlFoodService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.domain.CommonEntity;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Jzh
 * @Date: 2024-01-18  18:33
 */
@RestController
@Api(tags = "菜单管理")
@RequestMapping("/food")
public class TqlFoodController extends BaseController {

    @Autowired
    private TqlFoodService tqlFoodService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private HualalaApiService hualalaApiService;

    /**
     * 查询成本卡制作列表
     */
    @RequiresPermissions("bom:food:all")
    @GetMapping("/list")
    @ApiOperation("物料分页")
    public TableDataInfo list(TqlFood tqlFood) {
        startPage();
        List<TqlFood> list = tqlFoodService.selectFoodVoList(tqlFood);
        return getDataTable(list);
    }

    /**
     * 创建成本卡-查出单表所有产品
     */
    @RequiresPermissions("bom:food:all")
    @GetMapping("/findAllFood")
    public TableDataInfo findAllFood(@Parameter(hidden = true) @RequestParam Map<String, String> params) {

        String deptName = params.get("deptCode");
        if (!StringUtils.isEmpty(deptName) && deptName.length() > 12){
            params.put("deptCode",deptName.substring(0,12));
        }
        startPage();
        List<TqlFood> list = tqlFoodService.findAllFood(params);
        return getDataTable(list);
    }

    /**
     * 创建成本卡-根据foodid查出当前产品所对应的单位
     */
//    @RequiresPermissions("bom:food:findFoodUnit")
    @GetMapping("/{foodId}")
    public TableDataInfo findFoodUnit(@PathVariable("foodId") Long foodId){
        List<TqlFoodUnit> list = tqlFoodService.findFoodUnit(foodId);
        return getDataTable(list);
    }

    /**
     * 产品 当产品有成本卡之后不再展示
     */
    @RequiresPermissions("bom:food:all")
    @GetMapping("/query")
    public TableDataInfo foodList(TqlFood tqlFood){
        startPage();
        List<TqlFood> list = tqlFoodService.selectFoodList(tqlFood);
        return getDataTable(list);
    }




    @RequiresPermissions("bom:food:updateAll")
    @GetMapping("/updateAll")
    public AjaxResult updateAll(){

        try {
            LoginUser user = SecurityUtils.getLoginUser();
            SysUser sysUser = user.getSysUser();
            SysDept dept = sysDeptService.selectDeptById(sysUser.getDeptId());
            String deptCode = dept.getDeptCode();
            String shopId = sysDeptService.getEnabledHllShopIdByKingdeeFnumber(deptCode);
            hualalaApiService.saveShopMenu(Long.parseLong(shopId));

            return AjaxResult.success();
        }catch (Exception ex)
        {
            ex.printStackTrace();
            return  AjaxResult.error("更新失败");
        }

    }


    @RequiresPermissions("bom:food:setIsOpen")
    @GetMapping("/setIsOpen/{id}")
    public AjaxResult setIsOpen(@PathVariable String id){

        try {

            tqlFoodService.setIsOpen(Integer.parseInt(id));
            return AjaxResult.success();
        }catch (Exception ex)
        {
            ex.printStackTrace();
            return  AjaxResult.error("更新失败");
        }

    }




    @RequiresPermissions("bom:food:update")
    @GetMapping("/update")
    public AjaxResult update(@RequestParam("foodCode") String foodCode){

        try {
            LoginUser user = SecurityUtils.getLoginUser();
            SysUser sysUser = user.getSysUser();
            SysDept dept = sysDeptService.selectDeptById(sysUser.getDeptId());
            String deptCode = dept.getDeptCode();
            String shopId = sysDeptService.getEnabledHllShopIdByKingdeeFnumber(deptCode);
            CommonEntity entity = new CommonEntity();
            entity.setShopId(Long.parseLong(shopId));
            entity.setFoodCode(foodCode);
            hualalaApiService.updateFood(entity);

            return AjaxResult.success();
        }catch (Exception ex)
        {
            ex.printStackTrace();
            return  AjaxResult.error("更新失败");
        }

    }


    /**
     * 产品及价格信息
     */
    @RequiresPermissions("bom:food:all")
    @GetMapping("/foodAndPrice")
    public TableDataInfo selectFoodAndPriceList(TqlFood tqlFood) {
        LoginUser user = SecurityUtils.getLoginUser();
        SysUser sysUser = user.getSysUser();
        SysDept dept = sysDeptService.selectDeptById(sysUser.getDeptId());
       // SysDept dept=sysUser.getDept();
//        Long shopCode = dept.getRelateid();
        Integer shopId = dept.getRelateid();
        tqlFood.setShopId(String.valueOf(shopId));
        startPage();
        List<TqlFood> list = tqlFoodService.selectShopAndPriceList(tqlFood);
//        if(CollectionUtils.isEmpty(list)) {
//            if (shopId !=null) {
//                hualalaApiService.saveShopMenu(Long.valueOf(shopId));
//                list = tqlFoodService.selectShopAndPriceList(tqlFood);
//            }
//        }
        return getDataTable(list);
    }

}
